package datamining.algorithms.prefixspan;

import java.util.BitSet;
import java.util.Map;

import datamining.miner.enviroment.LocalEnvironment;
import datamining.miner.enviroment.ThreadEnvironment;
import datamining.miner.general.Embedding;
import datamining.miner.general.Fragment;
import datamining.miner.general.IntFrequency;
import datamining.miner.general.SequenceFragment;
import datamining.sequence.HPSequence;
import datamining.sequence.IndexedItem;
import datamining.sequence.Sequence;
import de.parsemis.algorithms.gSpan.GSpanHPEmbedding_flat;
import de.parsemis.miner.general.GraphBasedFragment;

/**
 * <p>
 * 创建于 2012-12-3 下午10:20:55
 * 
 * @author xujinghu (jinghuxu@nudt.edu.cn) <br>
 *         Copyright 2012 xujinghu <br>
 *         Lisence: Apache
 */
public class PThreadEnvironment<ItemType> extends ThreadEnvironment<ItemType> {

	SequenceFragment<ItemType> firstGHPFrag;
	public final int threadIdx;
	private boolean embeddingBased = true;
	private boolean usePooling = false;
	private PrefixSpanEmbedding<ItemType> firstEmbedding;
	private PrefixSpanExtension<ItemType> firstExtension;
	private IndexedItem<ItemType> firstItem;

	public PThreadEnvironment(final int threadIdx) {
		final LocalEnvironment<ItemType> env = LocalEnvironment.env();
		embeddingBased = env.embeddingBased;
		this.threadIdx = threadIdx;

	}

	public PrefixCode<ItemType> getCode(SequenceFragment<ItemType> frag,
			Sequence<ItemType> prefix) {
		return new PrefixCode<ItemType>(frag, prefix, this);
	}

	public PrefixSpanEmbedding<ItemType> getEmbedding(Sequence<ItemType> subSequence, PrefixSpanSequence<ItemType> sequence, Map<IndexedItem<ItemType>, IndexedItem<ItemType>> prefixMap) {
		return new PrefixSpanEmbedding<ItemType>(subSequence, sequence, prefixMap);
	}

	public PrefixSpanExtension<ItemType> getExtension(IndexedItem<ItemType> subItem,
			SequenceFragment<ItemType> fragment) {
		return new PrefixSpanExtension<ItemType>(subItem, fragment, this);
	}

	/**
	 * @author xujinghu
	 * @date 2012-12-9下午11:58:32
	 * @param sequence
	 * @return 包含空的embedding的fragment，其代表的序列为sequence
	 */
	public SequenceFragment<ItemType> getFragment(
			Sequence<ItemType> sequence) {
		return new SequenceFragment<ItemType>(new IntFrequency(0),
				sequence, new BitSet(), true);
	}

	@Override
	public void push(IndexedItem<ItemType> obj) {
		if (usePooling) {
			obj.next = firstItem;
			firstItem = obj;
		}
	}
	
	public final void push(final PrefixSpanEmbedding<ItemType> obj) {
		if (usePooling) {
			obj.next = firstEmbedding;
			firstEmbedding = obj;
		}
	}

	public final void push(final PrefixSpanExtension<ItemType> obj) {
		if (usePooling) {
			obj.next = firstExtension;
			firstExtension = obj;
		}
	}

}
